//
//  TIMSnapshotTextMessageCell.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/7/13.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMSnapshotTextMessageCell.h"
#import "Masonry.h"
#import "UIView+Extension.h"
#import "TIMDemoMacro.h"

#import "TIMMessage.h"

#define     MSG_SPACE_TOP       10
#define     MSG_SPACE_BTM       10
#define     MSG_SPACE_LEFT      10
#define     MSG_SPACE_RIGHT     15

@interface TIMSnapshotTextMessageCell ()

@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIImageView *SnapshotView;
@property (nonatomic, strong) UIImageView *SnapshotTImg;
@property (nonatomic, strong) UILabel *SnapshoteLabel;

@end

@implementation TIMSnapshotTextMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.messageLabel];
        [self.contentView addSubview:self.SnapshotView];
        [self.contentView addSubview:self.SnapshotTImg];
        [self.contentView addSubview:self.SnapshoteLabel];
        
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMsgBGView)];
        [self.messageBackgroundView addGestureRecognizer:tapGR];
    }
    return self;
}

- (void)setMessage:(TIMMessage *)message
{
    [super setMessage:message];
    self.messageLabel.text = message.text;
    [self.messageLabel setContentCompressionResistancePriority:500 forAxis:UILayoutConstraintAxisHorizontal];
    [self.messageBackgroundView setContentCompressionResistancePriority:100 forAxis:UILayoutConstraintAxisHorizontal];
    
    if (message.ownerTyper == TIMMessageOwnerTypeSelf) {
        self.messageLabel.hidden = NO;
        self.SnapshoteLabel.hidden = YES;
        self.SnapshotTImg.hidden = YES;
        self.messageLabel.textColor = [UIColor whiteColor];
        
        [self.messageBackgroundView setImage:[UIImage imageNamed:@"message_sender_bg"]];
        [self.messageBackgroundView setHighlightedImage:[UIImage imageNamed:@"message_sender_bgHL"]];
        
        [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.messageBackgroundView).mas_offset(-MSG_SPACE_RIGHT);
            make.top.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_TOP);
        }];
        [self.messageBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.messageLabel).mas_offset(-MSG_SPACE_LEFT);
            make.bottom.mas_equalTo(self.messageLabel).mas_offset(MSG_SPACE_BTM);
        }];
        
        [self.SnapshotView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.messageBackgroundView);
            make.centerX.mas_equalTo(self.messageBackgroundView.mas_left);
        }];
    }
    else if (message.ownerTyper == TIMMessageOwnerTypeFriend){
        self.messageLabel.textColor = RGBColor(51, 51, 51);
        
        [self.messageBackgroundView setImage:[UIImage imageNamed:@"message_receiver_bg"]];
        [self.messageBackgroundView setHighlightedImage:[UIImage imageNamed:@"message_receiver_bgHL"]];
        
        self.activityView.hidden = YES;
        self.retryButton.hidden = YES;
        if (message.clickState == TIMMessageUnClick) {
            self.messageLabel.hidden = YES;
            self.SnapshoteLabel.hidden = NO;
            self.SnapshotTImg.hidden = NO;
            self.SnapshoteLabel.text = @"阅后即焚";
            [self.SnapshoteLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_RIGHT);
                make.top.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_TOP);
            }];
            [self.messageBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.SnapshoteLabel).mas_offset(MSG_SPACE_RIGHT);
                make.bottom.mas_equalTo(self.SnapshoteLabel).mas_offset(MSG_SPACE_BTM);
            }];
            
            [self.SnapshotView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self.messageBackgroundView);
                make.centerX.mas_equalTo(self.messageBackgroundView.mas_right);
            }];
            [self.SnapshotTImg mas_updateConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self.SnapshoteLabel);
                make.right.mas_equalTo(self.messageBackgroundView.mas_right).mas_offset(-14);
            }];
            
        } else {
            self.messageLabel.hidden = NO;
            self.SnapshoteLabel.hidden = YES;
            self.SnapshotTImg.hidden = YES;
            
            [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_RIGHT);
                make.top.mas_equalTo(self.messageBackgroundView).mas_offset(MSG_SPACE_TOP);
            }];
            [self.messageBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(self.messageLabel).mas_offset(MSG_SPACE_RIGHT);
                make.bottom.mas_equalTo(self.messageLabel).mas_offset(MSG_SPACE_BTM);
            }];
            
            [self.SnapshotView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self.messageBackgroundView);
                make.centerX.mas_equalTo(self.messageBackgroundView.mas_right);
            }];
        }
    }
    
    // 3.1 设置文本最大尺寸
    CGSize textMaxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40 - 10 * 10, MAXFLOAT);
    // 3.2 计算文本真实尺寸
    CGSize textRealSize = [self sizeWithFont:[UIFont systemFontOfSize:15] maxSize:textMaxSize string:message.text];
    
    [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(textRealSize);
    }];
    
    if (message.ownerTyper == TIMMessageOwnerTypeSelf) {
        switch (message.sendState) {
            case TIMMessageSending:
            {
                [self.activityView setHidden:NO];
                [self.retryButton setHidden:YES];
                [self.activityView startAnimating];
            }
                break;
            case TIMMessageSendSuccess:
            {
                [self.activityView stopAnimating];
                [self.activityView setHidden:YES];
                [self.retryButton setHidden:YES];
                
            }
                break;
            case TIMMessageSendFail:
            {
                [self.activityView stopAnimating];
                [self.activityView setHidden:YES];
                [self.retryButton setHidden:NO];
            }
                break;
            default:
            {
                [self.activityView stopAnimating];
                [self.activityView setHidden:YES];
                [self.retryButton setHidden:YES];
            }
                break;
        }
    }
}

- (void)didTapMsgBGView
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(messageCellTap:)]) {
        [self.delegate messageCellTap:self.message];
        self.message.clickState = TIMMessageDidClick;
    }
}

/** 测量文本的尺寸 */
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize string:(NSString *)string {
    NSDictionary *attrs = @{NSFontAttributeName: font};
    CGSize size =  [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    return size;
}

#pragma mark - Getter -
- (UILabel *)messageLabel
{
    if (_messageLabel == nil) {
        _messageLabel = [[UILabel alloc] init];
        [_messageLabel setFont:TPSFontSize(AUTO_WIDTH(14))];
        [_messageLabel setNumberOfLines:0];
    }
    return _messageLabel;
}

- (UILabel *)SnapshoteLabel
{
    if (_SnapshoteLabel == nil) {
        _SnapshoteLabel = [[UILabel alloc] init];
        [_SnapshoteLabel setFont:TPSFontSize(AUTO_WIDTH(12))];
        [_SnapshoteLabel setNumberOfLines:1];
        _SnapshoteLabel.textColor = RGBColor(153, 153, 153);
        //        _SnapshoteLabel.text = @"点击查看";
    }
    return _SnapshoteLabel;
}

- (UIImageView *)SnapshotTImg
{
    if (_SnapshotTImg == nil) {
        _SnapshotTImg = [[UIImageView alloc] init];
        _SnapshotTImg.image = [UIImage imageNamed:@"Snapshot_title"];
    }
    return _SnapshotTImg;
}

- (UIImageView *)SnapshotView
{
    if (_SnapshotView == nil) {
        _SnapshotView = [[UIImageView alloc] init];
        _SnapshotView.image = [UIImage imageNamed:@"Snapshot_Image"];
    }
    return _SnapshotView;
}

@end
